package com.woniu.immunologicalmechanism.controller;

import com.woniu.common.entity.OrderInfo;
import com.woniu.common.entity.ResponseResult;
import com.woniu.immunologicalmechanism.service.ImmuneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.*;

//@Controller
//@Configuration
public class TimeUtil {

     @Autowired
      ImmuneService immuneService;



    //定时器
    //初始化变量
   // @PostConstruct
    public  void timeutil() {

        Calendar cal = Calendar.getInstance();
        //每天定点执行
        cal.set(Calendar.HOUR_OF_DAY, 3);
        cal.set(Calendar.MINUTE, 10);
        cal.set(Calendar.SECOND, 0);
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                //执行的内容
                System.out.println("执行定时任务");
                try {

                    //查询全部审核通过核验的信息
                List<OrderInfo> orderInfos = null;
                orderInfos = immuneService.getorderinfosBystatus(2);

                for (int i=0;i<orderInfos.size();i++){
                    String time=orderInfos.get(i).getCheck_time();
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Date date = null;

                        date = (Date) format.parse(time);
                        //有效期，时间加一年
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(date);//设置起时间
                    cal.add(Calendar.YEAR, 1);//增加一年


                     Date endtime =cal.getTime();
                     Date nowtime=new Date();
                    System.out.println(date);
                    System.out.println(endtime);
                    System.out.println(endtime.before(nowtime));//比较日期是否在nowtime之前
                    if (endtime.before(nowtime)){
                        //修改数据库状态，未年审
                        immuneService.updateorderInfostatus(orderInfos.get(i).getId(),3,null);

                    }
                }

                   // System.out.println(orderInfos);

                } catch (Exception e) {
                    e.printStackTrace();
                }
                //跟新缓存
                System.out.println("更新缓存");
                immuneService.update(1);
            }
        }, cal.getTime(), 24 * 60 * 60 * 1000);
    }

}
